package com.promise.controller;

import com.promise.controller.result.MonitorResult;
import com.promise.pojo.Monitor;
import com.promise.service.MonitorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/monitors")
public class MonitorController {
    @Autowired
    private MonitorServiceImpl monitorService;


    /**
     * 获取所有监控规则
     *
     * @return MonitorResult 包含所有监控规则的数据
     */
    @GetMapping("/all")
    public MonitorResult getAllMonitors() {
        List<Monitor> monitors = monitorService.list(); // 假设list()方法能从数据库获取所有监控规则
        Integer code = Code.SUCCESS_CODE;
        String msg = "获取所有监控规则成功";
        Map<String, Object> data = new HashMap<>();
        data.put("monitors", monitors);
        return new MonitorResult(code, data, msg);
    }

    /**
     * 添加警告规则
     *
     * @param monitorDto 要添加的警告规则
     * @return MonitorResult
     */
    @PostMapping("/add")
    @ResponseBody
    public MonitorResult add(@RequestBody Monitor monitorDto) {
        boolean flag = monitorService.save(monitorDto);
        Integer code = flag ? Code.SUCCESS_CODE : Code.ERROR_CODE;
        String msg = flag ? "添加成功" : "添加失败";
        Map<String, Object> data = new HashMap<>();
        data.put("monitor", monitorDto);
        return new MonitorResult(code, data, msg);
    }

    /**
     * 根据monitorID删除信息
     *
     * @param monitorId 用户的id
     * @return MonitorResult
     */
    @DeleteMapping("delete/{monitorId}")
    @ResponseBody
    public MonitorResult delete(@PathVariable Integer monitorId) {
        boolean flag = monitorService.removeById(monitorId);
        Integer code = flag ? Code.SUCCESS_CODE : Code.ERROR_CODE;
        String msg = flag ? "删除成功" : "删除失败";
        return new MonitorResult(code, flag, msg);
    }

    /**
     * 根据ID修改告警规则信息
     *
     * @param monitorId  要修改的告警规则信息的ID
     * @param monitorDto 包含新值告警规则信息的对象
     * @return MonitorResult
     */
    @PutMapping("update/{monitorId}")
    @ResponseBody
    public MonitorResult update(@PathVariable Integer monitorId, @RequestBody Monitor monitorDto) {
        // 检查传入的dutyDto是否有有效的ID，且与路径变量dutyId相匹配
        if (monitorDto.getMonitorId() == null || monitorDto.getMonitorId() != monitorId) {
            return new MonitorResult(Code.ERROR_CODE, null, "更新失败，提供的ID不匹配");
        }

        // 调用服务层的updateById方法来更新记录
        boolean isSuccess = monitorService.updateById(monitorDto);

        // 根据更新操作的成功与否构造DutyResult对象
        Map<String, Object> dataContainer = new HashMap<>();
        MonitorResult result;
        if (isSuccess) {
            dataContainer.put("monitor", monitorDto);
            result = new MonitorResult(Code.SUCCESS_CODE, dataContainer, "更新成功");
        } else {
            result = new MonitorResult(Code.ERROR_CODE, null, "更新失败");
        }
        return result;
    }

}
